<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * styles.php
 * Began: Thu January 16 2003
 * 
 * $Id: styles.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/
 
define('EQDKP_INC', true);
define('IN_ADMIN', true);
$eqdkp_root_path = './../';
include_once($eqdkp_root_path . 'common.php');

$fv = new Form_Validate;

// Obtain var settings
$style_id = ( !empty($_REQUEST['style']) ) ? intval($_REQUEST['style']) : '';
$confirm = ( !empty($_POST['confirm']) ) ? true : false;

// Start session management
$user->start();
$user->setup();
$user->check_auth('a_styles_man');

// Delete confirmation
if ( isset($_POST['cancel']) )
{
    if ( $style_id )
    {
        $redirect = 'styles.php'.$SID.'&style='.$style_id;
    }
    else
    {
        $redirect = 'styles.php'.$SID;
    }
    
    redirect($redirect);
}

// Figure out what submit button was pressed
$add    = ( isset($_POST['add']) ) ? true : false;
$update = ( isset($_POST['update']) ) ? true : false;
$delete = ( isset($_POST['delete']) ) ? true : false;

// Figure out action based on submit
if ( $add )
{
    $action = 'add';
}
elseif ( $update )
{
    $action = 'update';
}
elseif ( ($delete) || ($confirm) )
{
    $action = 'delete';
}
elseif ( ((isset($_GET['mode'])) && ($_GET['mode'] == 'create')) || ($style_id) )
{
    $action = 'display';
}
else
{
    $action = 'list';
}

if ( ($add) || ($update) || ($delete) )
{
    $_POST = htmlspecialchars_array($_POST);
}

if ( $style_id )
{
    $sql = 'SELECT s.*, c.*
            FROM ' . STYLES_TABLE . ' s, ' . STYLES_CONFIG_TABLE . " c
            WHERE (s.style_id = c.style_id)
            AND s.style_id='".$style_id."'";
    $result = $db->query($sql);
    if ( !$row = $db->fetch_record($result) )
    {
        message_die($user->lang['error_invalid_style_provided']);
    }
    $db->free_result($result);
    
    $style = array(
        'style_name' => post_or_db('style_name', true, $row),
        'template_path' => post_or_db('template_path', true, $row),
        'body_background' => post_or_db('body_background', true, $row),
        'body_link' => post_or_db('body_link', true, $row),
        'body_link_style' => post_or_db('body_link_style', true, $row),
        'body_hlink' => post_or_db('body_hlink', true, $row),
        'body_hlink_style' => post_or_db('body_hlink_style', true, $row),
        'header_link' => post_or_db('header_link', true, $row),
        'header_link_style' => post_or_db('header_link_style', true, $row),
        'header_hlink' => post_or_db('header_hlink', true, $row),
        'header_hlink_style' => post_or_db('header_hlink_style', true, $row),
        'tr_color1' => post_or_db('tr_color1', true, $row),
        'tr_color2' => post_or_db('tr_color2', true, $row),
        'th_color1' => post_or_db('th_color1', true, $row),
        'fontface1' => post_or_db('fontface1', true, $row),
        'fontface2' => post_or_db('fontface2', true, $row),
        'fontface3' => post_or_db('fontface3', true, $row),
        'fontsize1' => post_or_db('fontsize1', true, $row),
        'fontsize2' => post_or_db('fontsize2', true, $row),
        'fontsize3' => post_or_db('fontsize3', true, $row),
        'fontcolor1' => post_or_db('fontcolor1', true, $row),
        'fontcolor2' => post_or_db('fontcolor2', true, $row),
        'fontcolor3' => post_or_db('fontcolor3', true, $row),
        'fontcolor_neg' => post_or_db('fontcolor_neg', true, $row),
        'fontcolor_pos' => post_or_db('fontcolor_pos', true, $row),
        'table_border_width' => post_or_db('table_border_width', true, $row),
        'table_border_color' => post_or_db('table_border_color', true, $row),
        'table_border_style' => post_or_db('table_border_style', true, $row),
        'input_color' => post_or_db('input_color', true, $row),
        'input_border_width' => post_or_db('input_border_width', true, $row),
        'input_border_color' => post_or_db('input_border_color', true, $row),
        'input_border_style' => post_or_db('input_border_style', true, $row),
        'attendees_columns' => post_or_db('attendees_columns', true, $row),
        'date_notime_long' => post_or_db('date_notime_long', true, $row),
        'date_notime_short' => post_or_db('date_notime_short', true, $row),
        'date_time' => post_or_db('date_time', true, $row),
        'logo_path' => post_or_db('logo_path', true, $row)
    );
}
else
{
    $style = array(
        'style_name' => post_or_db('style_name', false),
        'template_path' => post_or_db('template_path', false),
        'body_background' => post_or_db('body_background', false),
        'body_link' => post_or_db('body_link', false),
        'body_link_style' => post_or_db('body_link_style', false),
        'body_hlink' => post_or_db('body_hlink', false),
        'body_hlink_style' => post_or_db('body_hlink_style', false),
        'header_link' => post_or_db('header_link', false),
        'header_link_style' => post_or_db('header_link_style', false),
        'header_hlink' => post_or_db('header_hlink', false),
        'header_hlink_style' => post_or_db('header_hlink_style', false),
        'tr_color1' => post_or_db('tr_color1', false),
        'tr_color2' => post_or_db('tr_color2', false),
        'th_color1' => post_or_db('th_color1', false),
        'fontface1' => post_or_db('fontface1', false),
        'fontface2' => post_or_db('fontface2', false),
        'fontface3' => post_or_db('fontface3', false),
        'fontsize1' => post_or_db('fontsize1', false),
        'fontsize2' => post_or_db('fontsize2', false),
        'fontsize3' => post_or_db('fontsize3', false),
        'fontcolor1' => post_or_db('fontcolor1', false),
        'fontcolor2' => post_or_db('fontcolor2', false),
        'fontcolor3' => post_or_db('fontcolor3', false),
        'fontcolor_neg' => post_or_db('fontcolor_neg', false),
        'fontcolor_pos' => post_or_db('fontcolor_pos', false),
        'table_border_width' => post_or_db('table_border_width', false),
        'table_border_color' => post_or_db('table_border_color', false),
        'table_border_style' => post_or_db('table_border_style', false),
        'input_color' => post_or_db('input_color', false),
        'input_border_width' => post_or_db('input_border_width', false),
        'input_border_color' => post_or_db('input_border_color', false),
        'input_border_style' => post_or_db('input_border_style', false),
        'attendees_columns' => post_or_db('attendees_columns', false),
        'date_notime_long' => post_or_db('date_notime_long', false),
        'date_notime_short' => post_or_db('date_notime_short', false),
        'date_time' => post_or_db('date_time', false),
        'logo_path' => post_or_db('logo_path', false)
    );
}

//
// Processing
//
switch ( $action )
{
    case 'add':
        extract($_POST);
        $sql = 'INSERT INTO ' . STYLES_TABLE . "
                (style_id, style_name, template_path, body_background, body_link, body_link_style, body_hlink, body_hlink_style,
                header_link, header_link_style, header_hlink, header_hlink_style, tr_color1, tr_color2, th_color1, fontface1,
                fontface2, fontface3, fontsize1, fontsize2, fontsize3, fontcolor1, fontcolor2, fontcolor3, fontcolor_neg,
                fontcolor_pos, table_border_width, table_border_color, table_border_style, input_color, input_border_width,
                input_border_color, input_border_style)
                VALUES ('NULL','$style_name','$template_path','$body_background','$body_link','$body_link_style','$body_hlink','$body_hlink_style',
                '$header_link','$header_link_style','$header_hlink','$header_hlink-style','$tr_color1','$tr_color2','$th_color1','$fontface1',
                '$fontface2','$fontface3','$fontsize1','$fontsize2','$fontsize3','$fontcolor1','$fontcolor2','$fontcolor3','$fontcolor_neg',
                '$fontcolor_pos','$table_border_width','$table_border_color','$table_border_style','$input_color','$input_border_width',
                '$input_border_color','$input_border_style')";
        $db->query($sql);
        
        $style_id = $db->insert_id();
        
        $sql = 'INSERT INTO ' . STYLES_CONFIG_TABLE . "
                (style_id, attendees_columns, date_notime_long, date_notime_short, date_time, logo_path)
                VALUES ('$style_id','$attendees_columns','$date_notime_long','$date_notime_short','$date_time','$logo_path')";
        $db->query($sql);
        
        message_die($user->lang['admin_add_style_success']);
                
        break;
    case 'update':
        extract($_POST);
        $sql = 'UPDATE ' . STYLES_TABLE . "
                SET style_name='$style_name', template_path='$template_path', body_background='$body_background', body_link='$body_link', body_link_style='$body_link_style',
                body_hlink='$body_hlink', body_hlink_style='$body_hlink_style', header_link='$header_link', header_link_style='$header_link_style', header_hlink='$header_hlink',
                header_hlink_style='$header_hlink_style', tr_color1='$tr_color1', tr_color2='$tr_color2', th_color1='$th_color1', fontface1='$fontface1', fontface2='$fontface2',
                fontface3='$fontface3', fontsize1='$fontsize1', fontsize2='$fontsize2', fontsize3='$fontsize3', fontcolor1='$fontcolor1', fontcolor2='$fontcolor2', fontcolor3='$fontcolor3',
                fontcolor_neg='$fontcolor_neg', fontcolor_pos='$fontcolor_pos', table_border_width='$table_border_width', table_border_color='$table_border_color', 
                table_border_style='$table_border_style', input_color='$input_color', input_border_width='$input_border_width', input_border_color='$input_border_color',
                input_border_style='$input_border_style'
                WHERE style_id='$style_id'";
        $db->query($sql);
        
        $sql = 'UPDATE ' . STYLES_CONFIG_TABLE . "
                SET attendees_columns='$attendees_columns', date_notime_long='$date_notime_long', date_notime_short='$date_notime_short', date_time='$date_time', logo_path='$logo_path'
                WHERE style_id='$style_id'";
        $db->query($sql);
        
        message_die($user->lang['admin_update_style_success']);
        
        break;
    case 'delete':
        if ( !$confirm )
        {
            confirm_delete($user->lang['confirm_delete_style'], 'style', $style_id, 'styles.php'.$SID);
        }
        else
        {
            $sql = 'DELETE FROM ' . STYLES_TABLE . "
                    WHERE style_id='".$style_id."'";
            $db->query($sql);
            
            $sql = 'DELETE FROM ' . STYLES_CONFIG_TABLE . "
                    WHERE style_id='".$style_id."'";
            $db->query($sql);
            
            message_die($user->lang['admin_delete_style_success']);
        }
        
        break;
    case 'display':
        $text_decoration = array(
            'none',
            'underline',
            'overline',
            'line-through',
            'blink');
        $border_style = array(
            'none',
            'hidden',
            'dotted',
            'dashed',
            'solid',
            'double',
            'groove',
            'ridge',
            'inset',
            'outset');
        
        // Available templates    
        if ( $dir = @opendir($eqdkp_root_path . 'templates/') )
        {
            while ( $file = @readdir($dir) )
            {
                if ( (!is_file($eqdkp_root_path . 'templates/' . $file)) && (!is_link($eqdkp_root_path . 'templates/' . $file)) && ($file != '.') && ($file != '..') && ($file != 'CVS') && ($file != 'cache') )
                {
                    $tpl->assign_block_vars('template_row', array(
                        'VALUE' => stripslashes($file),
                        'SELECTED' => '',
                        'OPTION' => stripslashes($file))
                    );
                }
            }
        }
        
        // Text decorations    
        foreach ( $text_decoration as $k => $v )
        {
            $tpl->assign_block_vars('body_link_style_row', array(
                'VALUE' => $v,
                'SELECTED' => ( $style['body_link_style'] == $v ) ? ' selected="selected"' : '',
                'OPTION' => $v)
            );
            $tpl->assign_block_vars('body_hlink_style_row', array(
                'VALUE' => $v,
                'SELECTED' => ( $style['body_hlink_style'] == $v ) ? ' selected="selected"' : '',
                'OPTION' => $v)
            );
            $tpl->assign_block_vars('header_link_style_row', array(
                'VALUE' => $v,
                'SELECTED' => ( $style['header_link_style'] == $v ) ? ' selected="selected"' : '',
                'OPTION' => $v)
            );
            $tpl->assign_block_vars('header_hlink_style_row', array(
                'VALUE' => $v,
                'SELECTED' => ( $style['header_hlink_style'] == $v ) ? ' selected="selected"' : '',
                'OPTION' => $v)
            );
        }
        
        // Border styles
        foreach ( $border_style as $k => $v )
        {
            $tpl->assign_block_vars('table_border_style_row', array(
                'VALUE' => $v,
                'SELECTED' => ( $style['table_border_style'] == $v ) ? ' selected="selected"' : '',
                'OPTION' => $v)
            );
            $tpl->assign_block_vars('input_border_style_row', array(
                'VALUE' => $v,
                'SELECTED' => ( $style['input_border_style'] == $v ) ? ' selected="selected"' : '',
                'OPTION' => $v)
            );
        }
        
        // Attendees columns
        for ( $i = 1; $i < 11; $i++)
        {
            $tpl->assign_block_vars('attendees_columns_row', array(
                'VALUE' => $i,
                'SELECTED' => ( $style['attendees_columns'] == $i ) ? ' selected="selected"' : '',
                'OPTION' => $i)
            );
        }
        
        $tpl->assign_vars(array(
            'F_ADD_STYLE' => 'styles.php'.$SID,
            'STYLE_ID' => $style_id,
            
            // Language
            'L_STYLE_SETTINGS' => $user->lang['style_settings'],
            'L_STYLE_NAME' => $user->lang['style_name'],
            'L_TEMPLATE' => $user->lang['template'],
            'L_ELEMENT' => $user->lang['element'],
            'L_VALUE' => $user->lang['value'],
            'L_BACKGROUND_COLOR' => $user->lang['background_color'],
            
            'L_FONTFACE1' => $user->lang['fontface1'],
            'L_FONTFACE1_NOTE' => $user->lang['fontface1_note'],
            'L_FONTFACE2' => $user->lang['fontface2'],
            'L_FONTFACE2_NOTE' => $user->lang['fontface2_note'],
            'L_FONTFACE3' => $user->lang['fontface3'],
            'L_FONTFACE3_NOTE' => $user->lang['fontface3_note'],
            
            'L_FONTSIZE1' => $user->lang['fontsize1'],
            'L_FONTSIZE1_NOTE' => $user->lang['fontsize1_note'],
            'L_FONTSIZE2' => $user->lang['fontsize2'],
            'L_FONTSIZE2_NOTE' => $user->lang['fontsize2_note'],
            'L_FONTSIZE3' => $user->lang['fontsize3'],
            'L_FONTSIZE3_NOTE' => $user->lang['fontsize3_note'],
            
            'L_FONTCOLOR1' => $user->lang['fontcolor1'],
            'L_FONTCOLOR1_NOTE' => $user->lang['fontcolor1_note'],
            'L_FONTCOLOR2' => $user->lang['fontcolor2'],
            'L_FONTCOLOR2_NOTE' => $user->lang['fontcolor2_note'],
            'L_FONTCOLOR3' => $user->lang['fontcolor3'],
            'L_FONTCOLOR3_NOTE' => $user->lang['fontcolor3_note'],
            
            'L_FONTCOLOR_NEG' => $user->lang['fontcolor_neg'],
            'L_FONTCOLOR_NEG_NOTE' => $user->lang['fontcolor_neg_note'],
            'L_FONTCOLOR_POS' => $user->lang['fontcolor_pos'],
            'L_FONTCOLOR_POS_NOTE' => $user->lang['fontcolor_pos_note'],
            
            'L_BODY_LINK' => $user->lang['body_link'],
            'L_BODY_LINK_STYLE' => $user->lang['body_link_style'],
            'L_BODY_HLINK' => $user->lang['body_hlink'],
            'L_BODY_HLINK_STYLE' => $user->lang['body_hlink_style'],
            'L_HEADER_LINK' => $user->lang['header_link'],
            'L_HEADER_LINK_STYLE' => $user->lang['header_link_style'],
            'L_HEADER_HLINK' => $user->lang['header_hlink'],
            'L_HEADER_HLINK_STYLE' => $user->lang['header_hlink_style'],
            
            'L_TR_COLOR1' => $user->lang['tr_color1'],
            'L_TR_COLOR2' => $user->lang['tr_color2'],
            'L_TH_COLOR1' => $user->lang['th_color1'],
            
            'L_TABLE_BORDER_WIDTH' => $user->lang['table_border_width'],
            'L_TABLE_BORDER_COLOR' => $user->lang['table_border_color'],
            'L_TABLE_BORDER_STYLE' => $user->lang['table_border_style'],
            'L_INPUT_COLOR' => $user->lang['input_color'],
            'L_INPUT_BORDER_WIDTH' => $user->lang['input_border_width'],
            'L_INPUT_BORDER_COLOR' => $user->lang['input_border_color'],
            'L_INPUT_BORDER_STYLE' => $user->lang['input_border_style'],
            
            'L_STYLE_CONFIGURATION' => $user->lang['style_configuration'],
            'L_STYLE_DATE_NOTE' => $user->lang['style_date_note'],
            'L_ATTENDEES_COLUMNS' => $user->lang['attendees_columns'],
            'L_ATTENDEES_COLUMNS_NOTE' => $user->lang['attendees_columns_note'],
            'L_DATE_NOTIME_LONG' => $user->lang['date_notime_long'],
            'L_DATE_NOTIME_SHORT' => $user->lang['date_notime_short'],
            'L_DATE_TIME' => $user->lang['date_time'],
            'L_LOGO_PATH' => $user->lang['logo_path'],
            
            'L_ADD_STYLE' => $user->lang['add_style'],
            'L_RESET' => $user->lang['reset'],
            'L_UPDATE_STYLE' => $user->lang['update_style'],
            'L_DELETE_STYLE' => $user->lang['delete_style'],
            
            // Data
            'STYLE_NAME' => $style['style_name'],
            'BODY_BACKGROUND' => $style['body_background'],
            'BODY_LINK' => $style['body_link'],
            'BODY_HLINK' => $style['body_hlink'],
            'HEADER_LINK' => $style['header_link'],
            'HEADER_HLINK' => $style['header_hlink'],
            'TR_COLOR1' => $style['tr_color1'],
            'TR_COLOR2' => $style['tr_color2'],
            'TH_COLOR1' => $style['th_color1'],
            'FONTFACE1' => $style['fontface1'],
            'FONTFACE2' => $style['fontface2'],
            'FONTFACE3' => $style['fontface3'],
            'FONTSIZE1' => $style['fontsize1'],
            'FONTSIZE2' => $style['fontsize2'],
            'FONTSIZE3' => $style['fontsize3'],
            'FONTCOLOR1' => $style['fontcolor1'],
            'FONTCOLOR2' => $style['fontcolor2'],
            'FONTCOLOR3' => $style['fontcolor3'],
            'FONTCOLOR_NEG' => $style['fontcolor_neg'],
            'FONTCOLOR_POS' => $style['fontcolor_pos'],
            'TABLE_BORDER_WIDTH' => $style['table_border_width'],
            'TABLE_BORDER_COLOR' => $style['table_border_color'],
            'TABLE_BORDER_STYLE' => $style['table_border_style'],
            'INPUT_COLOR' => $style['input_color'],
            'INPUT_BORDER_WIDTH' => $style['input_border_width'],
            'INPUT_BORDER_COLOR' => $style['input_border_color'],
            'INPUT_BORDER_STYLE' => $style['input_border_style'],
            'DATE_NOTIME_LONG' => $style['date_notime_long'],
            'DATE_NOTIME_SHORT' => $style['date_notime_short'],
            'DATE_TIME' => $style['date_time'],
            'STYLE_LOGO_PATH' => $style['logo_path'],
            
            'S_ADD' => ( !$style_id ) ? true : false)
        );
        
        $page_title = sprintf($user->lang['admin_title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['styles_title'];
        include_once($eqdkp_root_path . 'includes/page_header.php');
        
        $tpl->set_filenames(array(
            'body' => 'admin/addstyle.html')
        );
        
        include_once($eqdkp_root_path . 'includes/page_tail.php');
        break;
    case 'list':
        $sql = 'SELECT style_id, style_name, template_path, count(u.user_id) AS users
                FROM ( ' . STYLES_TABLE . ' s
                LEFT JOIN ' . USERS_TABLE . ' u
                ON u.user_style = s.style_id )
                GROUP BY s.style_id
                ORDER BY s.style_name';
        $result = $db->query($sql);
        while ( $row = $db->fetch_record($result) )
        {
            $tpl->assign_block_vars('styles_row', array(
                'ROW_CLASS' => $eqdkp->switch_row_class(),
                'U_EDIT_STYLE' => 'styles.php'.$SID.'&amp;style='.$row['style_id'],
                'NAME' => stripslashes($row['style_name']),
                'TEMPLATE' => $row['template_path'],
                'USERS' => $row['users'])
            );
        }
        $db->free_result($result);
        
        $tpl->assign_vars(array(
            'L_NAME' => $user->lang['name'],
            'L_TEMPLATE' => $user->lang['template'],
            'L_USERS' => $user->lang['users'])
        );
        
        $page_title = sprintf($user->lang['admin_title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['styles_title'];
        include_once($eqdkp_root_path . 'includes/page_header.php');
        
        $tpl->set_filenames(array(
            'body' => 'admin/styles.html')
        );
        
        include_once($eqdkp_root_path . 'includes/page_tail.php');
        break;
}
?>